package com.ujf.drools.model;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.ujf.drools.ProcessLauncher;

public class Ec2Cloud {

	private int nbInstance;
	private double lastTimeAdded;
	private double lastTimeRemoved;
	private int interval = 40 ;//ms //60*5


	private double nextTimeCanRemove;
	private double nextTimeCanAdd;
	
	public Ec2Cloud(int i, Properties props) {
		nbInstance = 0;
		lastTimeAdded = 0;
		lastTimeRemoved = 0;
		
		interval = Integer.parseInt(props.getProperty("interval"));
		System.out.println("Propriétés lues :");
		System.out.println("interval :"+interval);

	}

	public Ec2Cloud(int nbInstance) {
		this.nbInstance = nbInstance;
		lastTimeAdded = 0;
		lastTimeRemoved = 0;
	}

	public void removeInstance() {
		try {
			System.out.println("Removing an instance.");
			String[] args = { "/bin/sh", "-c",
					" ant -f un.deployEC2.xml STOPundeploy -Ddeploy.goal=leBonCoin  " };

//			 ProcessLauncher process = new ProcessLauncher();
//
//			 process.exec(args, null,new File("/home/ubuntu"));
			 
			Runtime runtime = Runtime.getRuntime();
			Process process;

			process = runtime.exec(args);

			StringWriter writer = new StringWriter();
			InputStreamReader streamReader = new InputStreamReader(
					process.getInputStream());
			BufferedReader buffer = new BufferedReader(streamReader);
			String line = "";

			while (null != (line = buffer.readLine())) {
				writer.write(line);
			}

			// Sortie finale dans le String
			String url = writer.toString();


			System.out.println("An instance was removed: <" + url + ">");

			nbInstance--;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void addInstance() {
		try {
			System.out.println("Adding an instance ...");
			String[] args = { "/bin/sh", "-c",
			" ant -f un.deployEC2.xml STOPdeploy -Ddeploy.goal=leBonCoin  " };			
			// Version grande sortie ->
//			 ProcessLauncher process = new ProcessLauncher();
//
//			 process.exec(args, null,new File("/home/ubuntu"));
			Runtime runtime = Runtime.getRuntime();
			final Process process = runtime.exec(args);

			StringWriter writer = new StringWriter();
			InputStreamReader streamReader = new InputStreamReader(
					process.getInputStream());
			
			BufferedReader buffer = new BufferedReader(streamReader);
			String line = "";

			while (null != (line = buffer.readLine())) {
				writer.write(line);
			}

			// Sortie finale dans le String
			String url = writer.toString();


			System.out.println("An instance was added : <" + url + ">");

			nbInstance++;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public double getLastTimeAdded() {
		return lastTimeAdded;
	}

	public void setLastTimeAdded(double lastTimeAdded) {
		this.lastTimeAdded = lastTimeAdded;
		this.nextTimeCanAdd=lastTimeAdded+interval;
		this.nextTimeCanRemove=lastTimeAdded+interval*2;
	}

	public double getLastTimeRemoved() {
		return lastTimeRemoved;
	}

	public void setLastTimeRemoved(double lastTimeRemoved) {
		this.lastTimeRemoved = lastTimeRemoved;
		this.nextTimeCanRemove=lastTimeRemoved+interval;

	}

	public int getNbInstance() {
		return nbInstance;
	}
	public int getInterval() {
		return interval;
	}

	public void setInterval(int interval) {
		this.interval = interval;
	}

	public double getNextTimeCanRemove() {
		return nextTimeCanRemove;
	}

	public void setNextTimeCanRemove(double nextTimeCanRemove) {
		this.nextTimeCanRemove = nextTimeCanRemove;
	}

	public double getNextTimeCanAdd() {
		return nextTimeCanAdd;
	}

	public void setNextTimeCanAdd(double nextTimeCanAdd) {
		this.nextTimeCanAdd = nextTimeCanAdd;
	}

	
}
